using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Data.Objects;
using DataAccess.BusinessEntities;

namespace DataAccess
{
    public class SqlRepository<T> : IRepository<T>
            where T : class, IEntity
    {
        public SqlRepository(ObjectContext context)
        {
            this.ObjectSet = context.CreateObjectSet<T>();
        }

        public IQueryable<T> FindAll()
        {
            return this.ObjectSet;//.Where(x => !x.IsDeleted);
        }

        public IQueryable<T> FindWhere(Expression<Func<T, bool>> predicate)
        {
            return this.ObjectSet.Where(predicate);
        }

        public T FindById(int id)
        {
            return this.ObjectSet.Single(o => o.Id == id);
        }

        public void Add(T entity)
        {
            this.ObjectSet.AddObject(entity);
        }

        public void Remove(T entity)
        {
            //this.ObjectSet.DeleteObject(entity);
            entity.IsDeleted = true;
        }

        protected ObjectSet<T> ObjectSet { get; set; }
    }
}
